package org.springcloud.wisdom.amusement.dao;

import org.apache.ibatis.annotations.*;
import org.springcloud.wisdom.amusement.entity.AmusementNotice;
import org.springcloud.wisdom.amusement.entity.Student;
import org.springcloud.wisdom.amusement.entity.TeamRecordsEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface TeamRecordsDao {

    //发起签到
    @Insert("insert into teamrecords(id, teamid, nid, sid,stu,signtime)" +
            "VALUES (#{id},#{teamid},#{nid},#{sid},#{stu},#{signtime})")
    int insertRecords(TeamRecordsEntity TeamRecordsEntity);

    //签到
    @Update("UPDATE teamrecords " +
            "SET stu=1, reacttime=#{reacttime} " +
            "WHERE id=#{id}")
    int updateRecords(TeamRecordsEntity TeamRecordsEntity);

    // 签到记录
    // 根据学生学号查询自己的签到记录
    // 获取个人的签到情况
    @Select("SELECT " +
            "r.*, " +
            "t.name, " +
            "n.title " +
            "FROM teamrecords r " +
            "INNER JOIN team t ON r.teamid = t.id " +
            "INNER JOIN notice n ON r.nid = n.id " +
            "WHERE r.sid = #{sid} " +
            "ORDER BY r.signtime DESC " +
            "LIMIT #{start}, #{size}")
    List<TeamRecordsEntity> get(@Param("sid") String sid, int start, int size);

    @Select("<script>" +
            "SELECT count(*) FROM teamrecords " +
            "WHERE sid = #{sid} " +
            "</script>")
    public int Counts( String sid);
    // 签到记录
    // 根据活动标题号查询本次活动的签到记录
    @Select("SELECT " +
            "r.*, " +
            "t.name, " +
            "n.title " +
            "FROM teamrecords r " +
            "INNER JOIN team t ON r.teamid = t.id " +
            "INNER JOIN notice n ON r.nid = n.id " +
            "WHERE r.nid = #{nid} " +
            "ORDER BY r.signtime DESC " +
            "LIMIT #{start}, #{size}")
    List<TeamRecordsEntity> get2(@Param("nid") Integer nid, int start, int size);

    @Select("<script>" +
            "SELECT count(*) FROM teamrecords " +
            "WHERE nid = #{nid} " +
            "</script>")
    public int Counts2( Integer nid);


    @Select("<script>" +
            "SELECT count(*) as shouldarrive FROM teamrecords " +
            "WHERE nid = #{nid} and teamid = #{teamid}" +
            "</script>")
    public int CountsShoudArrive(Integer nid,Integer teamid);
    @Select("<script>" +
            "SELECT count(*) as truearrive FROM teamrecords " +
            "WHERE nid = #{nid} and teamid = #{teamid} and stu=1" +
            "</script>")
    public int CountsTureArrive(Integer nid,Integer teamid);

    @Select("SELECT DISTINCT signtime FROM teamrecords" +
            " WHERE nid = #{nid} and teamid = #{teamid}")
    public String getTimeResult(Integer nid,Integer teamid);
    @Select("SELECT DISTINCT n.title FROM teamrecords r INNER JOIN notice n ON r.nid = n.id " +
            " WHERE r.nid = #{nid} and r.teamid = #{teamid}")
    public String getTitleResult(Integer nid,Integer teamid);

    @Select("SELECT * from student where sid in " +
            "(" +
            "SELECT sid as truearrive FROM teamrecords " +
            "WHERE nid = #{nid} and teamid = #{teamid} and stu=0 " +
            ")")
    public List<Student> getUnArriveStudent(Integer nid,Integer teamid);
    @Select("select  * from notice where ispublic = '同意' and type = #{type}")
    public List<AmusementNotice>  getTeamNotice(Integer type);


}
